package org.nicocube.airain.re.proc;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.nicocube.airain.domain.client.character.Action;
import org.nicocube.airain.domain.client.character.ActionType;
import org.nicocube.airain.domain.client.character.GameCharacter;
import org.nicocube.airain.re.utils.Alea;

public class ProcessorFactory {

	private Map<ActionType,Constructor<? extends Processor>> mapper = new HashMap<ActionType, Constructor<? extends Processor>>(); 
		
	public ProcessorFactory(Map<ActionType,Class<? extends Processor>> classes) throws SecurityException, NoSuchMethodException {
		for(Map.Entry<ActionType, Class<? extends Processor>> clazz : classes.entrySet()) {
			mapper.put(clazz.getKey(), clazz.getValue().getConstructor(GameCharacter.class,Action.class,Alea.class));	
		}
	}
	
	public Processor build(GameCharacter gc, Action action, Alea alea) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
		Constructor<? extends Processor> c = mapper.get(action.getType());
		return (Processor) c.newInstance(gc,action,alea);
	}

}
